﻿using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Phone.Controls;
using GalaSoft.MvvmLight.Messaging;

namespace MvvmLightNavgation
{
    public class NavigationHelper 
    {
        private static PhoneApplicationFrame _root;
         
        private static Dictionary<string, Uri> _pageDic;

        private static void GetPhoneFrameRoot()
        {
            if(_root==null)
            {
                _root = Application.Current.RootVisual as PhoneApplicationFrame;
                if(_root==null)
                {
                    throw new Exception("获取 ApplicationRootVisual 失败！");
                }
                else
                {

                    _root.Navigated += new System.Windows.Navigation.NavigatedEventHandler(RootNavigated);
                }
            }
        }

        private static void RootNavigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            string token = e.Uri.OriginalString;
            if (token.Contains("?"))
            {
                int index = e.Uri.OriginalString.IndexOf('?');
                token = token.Substring(0, index);
            }
            Messenger.Default.Send(e.Uri,token);
        }

        /// <summary>
        /// 注册page
        /// </summary>
        /// <param name="pageName"></param>
        /// <param name="url"></param>
        public static void RegPage(string pageName,string url)
        {
            if(_pageDic==null)
                _pageDic=new Dictionary<string, Uri>();
            if(!_pageDic.ContainsKey(pageName))
            {
                _pageDic.Add(pageName,CreateUri(url));
            }
        }

        /// <summary>
        /// 删除page
        /// </summary>
        /// <param name="pageName"></param>
        public  static void UnRegPage(string pageName)
        {
            if(_pageDic!=null)
            {
                _pageDic.Remove(pageName);
            }
        }

        /// <summary>
        /// 根据url字符串导航
        /// </summary>
        /// <param name="url"></param>
       public  static  void  NavigationTo(string url)
       {
           if (_root==null)
                GetPhoneFrameRoot();

            if(_root !=null)
            {
                var pageUri = new Uri(url, UriKind.Relative);
                _root.Navigate(pageUri);
            }

       }

        /// <summary>
        /// 根据Uri导航
        /// </summary>
        /// <param name="pageUri"></param>
        public  static void NavigationTo(Uri pageUri)
        {
            if (_root == null)
                GetPhoneFrameRoot();

            if (_root != null)
            {
                _root.Navigate(pageUri);
            }
        }

        /// <summary>
        /// 根据page名称导航
        /// 请先注册PageName
        /// </summary>
        /// <param name="pageName"></param>
        public  static  void NavigationByPageName(string pageName)
        {
            var pageUri = _pageDic[pageName];
            if (pageUri != null)
            {
                NavigationTo(pageUri);
            }
        }

        public static Uri CreateUri(string url)
        {
            return new Uri(url, UriKind.RelativeOrAbsolute);
        }

        /// <summary>
        /// 发送导航Msg
        /// </summary>
        /// <param name="pageUri"></param>
        public  static void NavigationMsgSend(Uri pageUri)
        {
            Messenger.Default.Send(pageUri, MsgToken.Navigation);
        }
        /// <summary>
        /// 发送导航Msg
        /// </summary>
        /// <param name="pageUrl"></param>
        public static void NavigationMsgSend(string pageUrl)
        {
            Messenger.Default.Send(CreateUri(pageUrl), MsgToken.Navigation);
        }

        /// <summary>
        /// 注册导航完成MSG
        /// </summary>
        public static  void NavigatedMsgReg(object recipient)
        {
            INavigation navigation = recipient as INavigation;
            if (navigation!=null)
            {
                Messenger.Default.Register<Uri>(recipient, navigation.GetViewUrl(), navigation.Navigated);
            }
            
        }

        public static string GetQueryString(Uri uri,String key)
        {
            if (uri.OriginalString.Contains(key))
            {
                string str = uri.OriginalString.Split('?')[1];
                str = str.Split('=')[1];
                return str;
            }
            else
                return "";
        }


    }
}
